<?php
global $_GPC, $_W;
$rid = intval($_GPC['rid']);
$uid = $_GPC['uid'];
$uniacid = $_W['uniacid'];


$fans = pdo_fetch("select * from " . tablename('haoman_dpm_fans') . " where rid = '" . $rid . "' and from_user='" . $uid . "'");

$bp = pdo_fetch("select is_messages,messagesinterval from " . tablename('haoman_dpm_hb_setting') . " where rid = :rid order by `id` desc", array(':rid' => $rid));

if(empty($bp)||$bp['is_messages']==1){
    $result = array(
        'code' => 3,
        'data' => "发送消息已被禁止！",
        'msg' => "发送消息已被禁止",
    );

    $this->message($result);
    exit();
}

if(empty($fans['avatar'])||$fans['avatar']=='/0'){
    $avatar = '../addons/haoman_dpm/images/item8.jpg';
}else{
    $avatar = $fans['avatar'];
}

$content = $_GPC['content'];

$image = $_GPC['image'];

$content = $this->emoji_encode($content);

if($bp['messagesinterval']>0){
    $messagesinterval = $bp['messagesinterval'];
    if($content){
        $last_bptime = pdo_fetch("select createtime from " . tablename('haoman_dpm_messages') . " where rid = :rid and from_user = :from_user and is_bp = 0  and  word !='' order by `id` desc", array(':rid' => $rid,':from_user'=>$fans['from_user']));
    }
    if($image){
        $last_bptime = pdo_fetch("select createtime from " . tablename('haoman_dpm_messages') . " where rid = :rid and from_user = :from_user and is_bp = 0  and wordimg !='' order by `id` desc", array(':rid' => $rid,':from_user'=>$fans['from_user']));
    }

    if(time()-$last_bptime['createtime']<$messagesinterval){
        $result = array(
            'code' => 4,
            'data' => "请不要频繁发送消息或者图片！",
            'msg' => "请不要频繁发送消息或者图片",
        );

        $this->message($result);
        exit();
    }
}else{
    $messagesinterval = 0;
}

$insert = array(
    'uniacid' => $uniacid,
    'avatar' => $avatar,
    'nickname' => $fans['nickname'],
    'from_user' => $fans['from_user'],
    'word' => $content,
    'wordimg' => $image,
    'rid' => $rid,
    'status' => 1,
    'is_back' => $fans['is_back'],
    'is_xy' =>0,
    'is_bp' =>0,
    'type' =>0,
    'gift' =>0,
    'createtime' => time(),
);
$temp = pdo_insert('haoman_dpm_messages',$insert);

pdo_update('haoman_dpm_fans', array('last_onlinetime' => time()), array('id' => $fans['id']));

$result = array(
    'code' => 8,
    'data' => "信息已上墙，请关注大屏幕！",
    'msg' => "信息已上墙，请关注大屏幕！",
    'times' => $messagesinterval,

);

$this->message($result);